import { guid } from "../utils";
import { getScale } from "@/graphviewState";
export default class GraphBase {
  id!: number;
  tag: string;
  ctx!: CanvasRenderingContext2D | null;
  type = "";
  // 图形缩放比
  zoom: number = 1;
  // 图形旋转角度
  rotation: number = 0;
  constructor() {
    this.tag = guid();
  }
  setId(id: number) {
    this.id = id;
  }
  getId() {
    return this.id;
  }
  getTag() {
    return this.tag;
  }
  setCtx(ctx: CanvasRenderingContext2D | null) {
    this.ctx = ctx;
  }
  // 基类方法,子类实现
  draw() {}
  // 图形渲染,先把画布的缩放比例应用上,绘制结束后恢复
  render() {
    if (this.ctx) {
      this.ctx.save();
      const scale = getScale();
      this.ctx.scale(scale, scale);
      this.draw();
      this.ctx.restore();
    }
  }
  // 基类方法,子类实现
  contain(x: number, y: number): boolean {
    return false;
  }
  // 基类方法,子类实现
  getRect(): {
    x: number;
    y: number;
    width: number;
    height: number;
  } {
    return { x: 0, y: 0, width: 0, height: 0 };
  }
  // 从原始位置偏移
  translate(x: number, y: number) {}
  // 从当前位置偏移
  offset(x: number, y: number) {}
  // 尺寸缩放
  scale(width: number, height: number) {}
  changeProperty(propertyName: string, propertyValue: any) {}
  setOriginRect(x: number, y: number, width: number, height: number) {}
  getOriginRect() {
    return {
      x: 0,
      y: 0,
      width: 0,
      height: 0,
    };
  }
  setZoom(zoom: number) {}
}
